/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.zimbra.common.calendar.ICalTimeZone;
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.ReplyInfo;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.mailbox.Metadata;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.Util;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.Mime.FixedMimeMessage;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SetCalendarItem
/*     */   extends RedoableOp
/*     */   implements CreateCalendarItemRecorder, CreateCalendarItemPlayer
/*     */ {
/*     */   private int mFolderId;
/*     */   private int mCalendarItemId;
/*  53 */   private String mCalendarItemPartStat = "NE";
/*     */   private boolean mAttachmentIndexingEnabled;
/*     */   private int mFlags;
/*     */   private String[] mTags;
/*     */   private long mTagBitmask;
/*     */   private Mailbox.SetCalendarItemData mDefaultInvite;
/*     */   private Mailbox.SetCalendarItemData[] mExceptions;
/*     */   private List<CalendarItem.ReplyInfo> mReplies;
/*     */   private long mNextAlarm;
/*     */   
/*     */   public SetCalendarItem() {
/*  64 */     super(MailboxOperation.SetCalendarItem);
/*     */   }
/*     */   
/*     */   private void serializeSetCalendarItemData(RedoLogOutput out, Mailbox.SetCalendarItemData data) throws IOException
/*     */   {
/*  69 */     out.writeBoolean(true);
/*     */     
/*     */ 
/*  72 */     ICalTimeZone localTz = data.invite.getTimeZoneMap().getLocalTimeZone();
/*  73 */     out.writeUTF(Util.encodeAsMetadata(localTz).toString());
/*     */     
/*  75 */     out.writeUTF(Invite.encodeMetadata(data.invite).toString());
/*     */     
/*  77 */     if (getVersion().atLeast(1, 24)) {
/*  78 */       out.writeBoolean(data.message != null);
/*     */     }
/*     */     
/*  81 */     if (data.message != null) {
/*  82 */       out.writeLong(data.message.getReceivedDate());
/*  83 */       byte[] pmData = data.message.getRawData();
/*  84 */       out.writeInt(pmData.length);
/*  85 */       out.write(pmData);
/*     */     }
/*     */   }
/*     */   
/*     */   private Mailbox.SetCalendarItemData deserializeSetCalendarItemData(RedoLogInput in, boolean attachmentIndexingEnabled) throws IOException, MessagingException
/*     */   {
/*  91 */     Mailbox.SetCalendarItemData toRet = new Mailbox.SetCalendarItemData();
/*     */     
/*  93 */     int mboxId = getMailboxId();
/*     */     try {
/*  95 */       in.readBoolean();
/*     */       
/*     */ 
/*  98 */       ICalTimeZone localTz = Util.decodeTimeZoneFromMetadata(new Metadata(in.readUTF()));
/*     */       
/* 100 */       toRet.invite = Invite.decodeMetadata(mboxId, new Metadata(in.readUTF()), null, localTz);
/*     */       boolean hasPm;
/*     */       boolean hasPm;
/* 103 */       if (getVersion().atLeast(1, 24)) {
/* 104 */         hasPm = in.readBoolean();
/*     */       } else {
/* 106 */         hasPm = true;
/*     */       }
/*     */       
/* 109 */       if (hasPm) {
/* 110 */         long receivedDate = in.readLong();
/* 111 */         int dataLen = in.readInt();
/* 112 */         byte[] rawPmData = new byte[dataLen];
/* 113 */         in.readFully(rawPmData, 0, dataLen);
/*     */         
/* 115 */         InputStream is = new SharedByteArrayInputStream(rawPmData);
/* 116 */         MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), is);
/*     */         
/* 118 */         toRet.message = new ParsedMessage(mm, receivedDate, attachmentIndexingEnabled);
/*     */       }
/*     */     } catch (ServiceException ex) {
/* 121 */       ex.printStackTrace();
/* 122 */       throw new IOException("Cannot read serialized entry for CreateInvite " + ex.toString());
/*     */     }
/*     */     
/* 125 */     return toRet;
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException {
/* 129 */     assert (getMailboxId() != 0);
/* 130 */     out.writeInt(this.mFolderId);
/* 131 */     if (getVersion().atLeast(1, 0)) {
/* 132 */       out.writeShort((short)-1);
/*     */     }
/* 134 */     out.writeInt(this.mCalendarItemId);
/* 135 */     if (getVersion().atLeast(1, 1)) {
/* 136 */       out.writeUTF(this.mCalendarItemPartStat);
/*     */     }
/* 138 */     if (getVersion().atLeast(1, 2)) {
/* 139 */       out.writeBoolean(this.mAttachmentIndexingEnabled);
/*     */     }
/* 141 */     if (getVersion().atLeast(1, 11)) {
/* 142 */       out.writeInt(this.mFlags);
/* 143 */       if (getVersion().atLeast(1, 33)) {
/* 144 */         out.writeUTFArray(this.mTags);
/*     */       } else {
/* 146 */         out.writeLong(this.mTagBitmask);
/*     */       }
/*     */     }
/*     */     
/* 150 */     boolean hasDefaultInvite = this.mDefaultInvite != null;
/* 151 */     if (getVersion().atLeast(1, 17))
/* 152 */       out.writeBoolean(hasDefaultInvite);
/* 153 */     if (hasDefaultInvite) {
/* 154 */       serializeSetCalendarItemData(out, this.mDefaultInvite);
/*     */     }
/*     */     
/* 157 */     if (this.mExceptions == null) {
/* 158 */       out.writeInt(0);
/*     */     } else {
/* 160 */       out.writeInt(this.mExceptions.length);
/* 161 */       for (int i = 0; i < this.mExceptions.length; i++) {
/* 162 */         serializeSetCalendarItemData(out, this.mExceptions[i]);
/*     */       }
/*     */     }
/*     */     
/* 166 */     if (getVersion().atLeast(1, 15)) {
/* 167 */       if (this.mReplies == null)
/*     */       {
/*     */ 
/*     */ 
/* 171 */         out.writeInt(-1);
/*     */       } else {
/* 173 */         int num = this.mReplies.size();
/* 174 */         out.writeInt(num);
/* 175 */         for (CalendarItem.ReplyInfo ri : this.mReplies) {
/* 176 */           out.writeUTF(ri.encodeAsMetadata().toString());
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 181 */     if (getVersion().atLeast(1, 21)) {
/* 182 */       out.writeLong(this.mNextAlarm);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in) throws IOException {
/* 187 */     this.mFolderId = in.readInt();
/* 188 */     if (getVersion().atLeast(1, 0)) {
/* 189 */       in.readShort();
/*     */     }
/* 191 */     this.mCalendarItemId = in.readInt();
/* 192 */     if (getVersion().atLeast(1, 1)) {
/* 193 */       this.mCalendarItemPartStat = in.readUTF();
/*     */     }
/* 195 */     if (getVersion().atLeast(1, 2)) {
/* 196 */       this.mAttachmentIndexingEnabled = in.readBoolean();
/*     */     } else {
/* 198 */       this.mAttachmentIndexingEnabled = false;
/*     */     }
/* 200 */     if (getVersion().atLeast(1, 11)) {
/* 201 */       this.mFlags = in.readInt();
/* 202 */       if (getVersion().atLeast(1, 33)) {
/* 203 */         this.mTags = in.readUTFArray();
/*     */       } else {
/* 205 */         this.mTagBitmask = in.readLong();
/*     */       }
/*     */     }
/*     */     
/* 209 */     Invite tzmapInv = null;
/* 210 */     boolean hasDefaultInvite = true;
/* 211 */     if (getVersion().atLeast(1, 17)) {
/* 212 */       hasDefaultInvite = in.readBoolean();
/*     */     }
/*     */     try {
/* 215 */       if (hasDefaultInvite) {
/* 216 */         this.mDefaultInvite = deserializeSetCalendarItemData(in, this.mAttachmentIndexingEnabled);
/* 217 */         tzmapInv = this.mDefaultInvite.invite;
/*     */       }
/* 219 */       int numExceptions = in.readInt();
/* 220 */       if (numExceptions > 0) {
/* 221 */         this.mExceptions = new Mailbox.SetCalendarItemData[numExceptions];
/* 222 */         for (int i = 0; i < numExceptions; i++) {
/* 223 */           this.mExceptions[i] = deserializeSetCalendarItemData(in, this.mAttachmentIndexingEnabled);
/* 224 */           if (tzmapInv == null) {
/* 225 */             tzmapInv = this.mExceptions[i].invite;
/*     */           }
/*     */         }
/*     */       }
/*     */     } catch (MessagingException ex) {
/* 230 */       ex.printStackTrace();
/* 231 */       throw new IOException("Cannot read serialized entry for SetCalendarItem" + ex.toString());
/*     */     }
/*     */     
/* 234 */     if (getVersion().atLeast(1, 15)) {
/* 235 */       int num = in.readInt();
/* 236 */       if ((num > 10000) && (!getVersion().atLeast(1, 24)))
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 244 */         throw new IOException("Replies count > 10000.  Looks like a corrupted pre-v1.24 redo entry.  Skipping");
/*     */       }
/* 246 */       if (num < 0)
/*     */       {
/* 248 */         this.mReplies = null;
/*     */       } else {
/* 250 */         this.mReplies = new ArrayList(num);
/* 251 */         TimeZoneMap tzMap = tzmapInv.getTimeZoneMap();
/* 252 */         for (int i = 0; i < num; i++) {
/* 253 */           String data = in.readUTF();
/*     */           try {
/* 255 */             Metadata md = new Metadata(data);
/* 256 */             CalendarItem.ReplyInfo reply = CalendarItem.ReplyInfo.decodeFromMetadata(md, tzMap);
/* 257 */             this.mReplies.add(reply);
/*     */           } catch (ServiceException e) {
/* 259 */             IOException ioe = new IOException("Cannot read serialized entry for ReplyInfo");
/* 260 */             ioe.initCause(e);
/* 261 */             throw ioe;
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 267 */     if (getVersion().atLeast(1, 21)) {
/* 268 */       this.mNextAlarm = in.readLong();
/*     */     }
/*     */   }
/*     */   
/*     */   public SetCalendarItem(int mailboxId, boolean attachmentIndexingEnabled, int flags, String[] tags) {
/* 273 */     this();
/* 274 */     setMailboxId(mailboxId);
/* 275 */     this.mAttachmentIndexingEnabled = attachmentIndexingEnabled;
/* 276 */     this.mFlags = flags;
/* 277 */     this.mTags = tags;
/*     */   }
/*     */   
/*     */ 
/*     */   public void setData(Mailbox.SetCalendarItemData defaultInvite, Mailbox.SetCalendarItemData[] exceptions, List<CalendarItem.ReplyInfo> replies, long nextAlarm)
/*     */   {
/* 283 */     this.mDefaultInvite = defaultInvite;
/* 284 */     this.mExceptions = exceptions;
/* 285 */     this.mReplies = replies;
/* 286 */     this.mNextAlarm = nextAlarm;
/*     */   }
/*     */   
/*     */   public Mailbox.SetCalendarItemData getDefaultData() {
/* 290 */     return this.mDefaultInvite;
/*     */   }
/*     */   
/*     */   public int getNumExceptions() {
/* 294 */     if (this.mExceptions == null) {
/* 295 */       return 0;
/*     */     }
/* 297 */     return this.mExceptions.length;
/*     */   }
/*     */   
/*     */   public Mailbox.SetCalendarItemData getExceptionData(int exceptionNum) {
/* 301 */     return this.mExceptions[exceptionNum];
/*     */   }
/*     */   
/*     */   public void setCalendarItemAttrs(int calItemId, int folderId)
/*     */   {
/* 306 */     this.mCalendarItemId = calItemId;
/* 307 */     this.mFolderId = folderId;
/*     */   }
/*     */   
/*     */   public int getCalendarItemId()
/*     */   {
/* 312 */     return this.mCalendarItemId;
/*     */   }
/*     */   
/*     */   public String getCalendarItemPartStat()
/*     */   {
/* 317 */     return this.mCalendarItemPartStat;
/*     */   }
/*     */   
/*     */   public void setCalendarItemPartStat(String partStat)
/*     */   {
/* 322 */     this.mCalendarItemPartStat = partStat;
/*     */   }
/*     */   
/*     */   public int getFolderId()
/*     */   {
/* 327 */     return this.mFolderId;
/*     */   }
/*     */   
/*     */   protected String getPrintableData()
/*     */   {
/* 332 */     StringBuffer toRet = new StringBuffer();
/* 333 */     toRet.append("calItemId=").append(this.mCalendarItemId);
/* 334 */     toRet.append(", calItemPartStat=").append(this.mCalendarItemPartStat);
/* 335 */     toRet.append(", folder=").append(this.mFolderId);
/* 336 */     if (getVersion().atLeast(1, 11)) {
/* 337 */       toRet.append(", flags=").append(this.mFlags);
/* 338 */       toRet.append(", tags=").append(TagUtil.encodeTags(this.mTags));
/*     */     }
/* 340 */     toRet.append("\n");
/* 341 */     if (this.mDefaultInvite != null) {
/* 342 */       toRet.append("Default=").append(this.mDefaultInvite.toString()).append("\n");
/*     */     }
/* 344 */     if (this.mExceptions != null) {
/* 345 */       for (int i = 0; i < this.mExceptions.length; i++)
/* 346 */         toRet.append("Exception").append(i).append("=").append(this.mExceptions[i].toString()).append("\n");
/*     */     }
/*     */     int i;
/* 349 */     if (this.mReplies != null) {
/* 350 */       i = 0;
/* 351 */       for (CalendarItem.ReplyInfo ri : this.mReplies) {
/* 352 */         toRet.append("Reply").append(i).append("=").append(ri.toString()).append("\n");
/* 353 */         i++;
/*     */       }
/*     */     }
/* 356 */     toRet.append("nextAlarm=").append(this.mNextAlarm).append("\n");
/* 357 */     return toRet.toString();
/*     */   }
/*     */   
/*     */   public void redo() throws Exception
/*     */   {
/* 362 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(getMailboxId());
/* 363 */     OperationContext octxt = getOperationContext();
/*     */     
/* 365 */     if ((this.mTags == null) && (this.mTagBitmask != 0L)) {
/* 366 */       this.mTags = TagUtil.tagBitmaskToNames(mbox, octxt, this.mTagBitmask);
/*     */     }
/*     */     
/* 369 */     mbox.setCalendarItem(octxt, this.mFolderId, this.mFlags, this.mTags, this.mDefaultInvite, this.mExceptions, this.mReplies, this.mNextAlarm);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/SetCalendarItem.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */